package cn.shen.edit.service.Imp;

import cn.hutool.core.util.ObjectUtil;
import cn.shen.common.util.WebResultUtil;
import cn.shen.common.vo.WebResult;
import cn.shen.dao.entity.User;
import cn.shen.dao.mapper.UserMapper;
import cn.shen.edit.service.UserService;
import cn.shen.edit.util.JwtUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;


@Service
public class UserServiceImp implements UserService {

    @Autowired
    private UserMapper userMapper;
//    @Autowired
//    private JwtUtil jwtUtil;

    @Override
    public WebResult checkLogin(User vo) {
        User user = userMapper.selectOne(new LambdaQueryWrapper<User>()
                .eq(User::getUsername, vo.getUsername())
        );
        if (ObjectUtil.isNotEmpty(user)){
            boolean equal = ObjectUtil.equal(vo.getPassword(), user.getPassword());
            if (equal){
                String userToke = JwtUtil.createUserToke(user);
                return WebResultUtil.genSuccessResult("登录成功",userToke);
            }else {
                return WebResultUtil.genErrorResult("密码错误");
            }
        }else {
            return WebResultUtil.genErrorResult("用户名不存在");
        }
    }
}
